import { getToken, setToken, removeToken } from '@/utils/auth'
import { login,getUserInfo } from '@/api/user'
const state = {
  token: getToken(),
  userInfo:{} , // 用户信息资料
}

const mutations = {
  setToken(state, token) {
    state.token = token
    // 同步引入到缓存
    setToken(token)
  },

  removeToken(state) {
    state.token = null
    removeToken()
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
}

const actions = {
  async login(ctx, data) {
    console.log('data', data);
    let {mobile,password} = data
    let token = await login(mobile,password)
    ctx.commit('setToken', token)
  },
  // 获取用户的基本资料
  async getUserInfo(ctx){
    let userInfo = await getUserInfo()
    console.log('userInfo',userInfo);
    ctx.commit('setUserInfo', userInfo)
  },
   // 退出登录
   logout(ctx){
    ctx.commit('removeToken')
    ctx.commit('setUserInfo',{})
  }
}



export default {
  namespaced: true,
  state,
  mutations,
  actions
}